import { defineComponent, ref } from 'vue';
import request from '@/request'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'

export default defineComponent({
  name: "login",
  setup() {
    const store = useStore()
    const route = useRoute()
    const router = useRouter()
    const userName = ref('')
    const password = ref('')
    // 登录
    const fnDoLogin = () => {
      request('/api/login', 'POST', { userName: userName.value, password: password.value }).then(
        (res: any) => {
          if (res.code === 200) {
            localStorage.setItem("token", res.token)
            store.commit('SET_USERINFO', res.user)
            router.push('/')
          } else {
            ElMessage.error(res.msg)
          }
        },
      )
    }
    const doLogin = () => {
      sessionStorage.setItem("token", '111')
      router.push('/')
    }
    return {
      route,
      fnDoLogin,
      userName,
      password,
      doLogin,
    }
  },
  methods: {},
  components: {},

})

